//
//  MyIncomeNoteViewController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/9/29.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//
#import "MySubStoreViewController.h"
#import "MyIncomeNoteViewController.h"
#import "EarningsCell.h"
#import "ShareProfitModel.h"
@interface MyIncomeNoteViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataListArr;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)int pageindex;
@property(nonatomic,assign)int counts;
@property(nonatomic,assign)NSInteger pageindexCount;


@end

@implementation MyIncomeNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageindex = 1;
    self.counts = 10;
    [self createTableView];
    [self refresh];
}
-(void)refresh{
    
    __unsafe_unretained __typeof(self) weakSelf = self;
    
        // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
        _tableView.mj_header = [MJRefreshSHNormalHeader DYHeaderWithRefreshingBlock:^{
            _pageindex = 1;
            _pageindexCount = 0;

            [weakSelf requestDataIsRemove:YES];
        }];
    
        // 马上进入刷新状态
        [_tableView.mj_header beginRefreshing];
    
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_pageindexCount < 10) {
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView.mj_footer setState:MJRefreshStateNoMoreData];
            return ;
        }
        self.pageindex ++;
        [weakSelf requestDataIsRemove:NO];
    
        
    }];
    
}


#pragma mark -- 初始化数组
-(NSMutableArray *)dataListArr{
    if (!_dataListArr) {
        _dataListArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataListArr;
}
#define KEarningsCell        @"EarningsCell"
#pragma mark -- 创建TableView
-(void)createTableView{
    
    self.view.backgroundColor = KBASEBGCOLOR;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = KBASEBGCOLOR;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[EarningsCell class] forCellReuseIdentifier:KEarningsCell];
    self.tableView.separatorStyle =   UITableViewCellSeparatorStyleNone;
    if (self.source != 13) return ;
    UIBarButtonItem *mySubStore = [[UIBarButtonItem alloc]initWithTitle:@"我的商家" style:UIBarButtonItemStyleDone target:self action:@selector(lookMySubStore)];
    [mySubStore setTitleTextAttributes:@{NSFontAttributeName:KSystemFont(__I6Width(13))} forState:UIControlStateNormal];
    mySubStore.width = - 10;
    self.navigationItem.rightBarButtonItems = @[mySubStore];

}

#pragma mark --- 查看我的商家
- (void)lookMySubStore{
    
    MySubStoreViewController *mySubStoreVc = [[MySubStoreViewController alloc]init];
    [self.navigationController pushViewController:mySubStoreVc animated:YES];
    
    
    
}

#pragma mark -- 获取数据
-(void)requestDataIsRemove:(BOOL)isRemove{
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:[NSString stringWithFormat:self.urlString, self.source,self.pageindex,self.counts] httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([result[@"success"] boolValue]) {
            NSArray *data = result[@"data"];
            _pageindexCount = data.count;
        }
        if ([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] count] != 0) {
            if (isRemove) {
                [self.dataListArr removeAllObjects];
            }
            
            for (NSDictionary *dict in result[@"data"]) {
                ShareProfitModel *model = [[ShareProfitModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                
               
                [self.dataListArr addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else if([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] count] == 0){

            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];

        }
        
        
    } failure:^(id erro) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
    
    
    
    
}

#pragma mark --  UITableViewDataSourece && UITableDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataListArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EarningsCell *cell = [[EarningsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KEarningsCell];
    if (self.dataListArr.count != 0) {
          cell.shareProfitModel = self.dataListArr[indexPath.row];
    }
 
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(65);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   ShareProfitModel *model =  self.dataListArr[indexPath.row];

    [Tool alertWithMessage:model.memo  title:@"详情" who:self];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
